//
//  LanBSocketCppController.m
//  LanBKit_Example
//
//  Created by v on 2020/11/2.
//  Copyright © 2020 v. All rights reserved.
//

#import "LanBSocketCppController.h"
#import <MJExtension.h>
#import <LanBSio.h>
#import <Toast.h>

@interface LanBSocketCppController ()
@property (nonatomic, strong) LanBSio *lanbSio;
@end

@implementation LanBSocketCppController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    @weakify(self)
    [self.lanbSio handleStatus:^(LanBSioStatus status) {
        @strongify(self);
        NSString *str = [NSString stringWithFormat:@"%@,\n链接状态:%lu" ,self.lanbSio, (unsigned long)status];
        NSLog(@"%@" ,str);
        [self.view makeToast:str
        duration:3.f
        position:CSToastPositionCenter];
    }];
    
    
}

- (IBAction)onClickSocket:(id)sender {
    [self.lanbSio connect];
}

- (IBAction)onCloseSocket:(id)sender {
    [self.lanbSio disConnect];
}

- (IBAction)onStatusSocket:(id)sender {
    @weakify(self)
    
//    NSArray *ary = @[
//                    @1,
//                    @"2",
//                    [NSNumber numberWithBool:YES],
//                    @4,
//                    @"55555",
//                    @{@"objc6" : @{
//                              @"map1":@"map1",
//                              @"map2":@2,
//                              @"map3":@"map3",
//                    }},
//                    @[@"ary7",@"ary2",@"ary3",@"ary4",@"ary5",@6],
//                    [@"123" dataUsingEncoding:NSUTF8StringEncoding],
//                    ];
//    sio::message::list msglist0 = [self _transferToCppList:ary];
//    NSArray *a = [self _transferToObjCList:msglist0];
//    NSLog(@"%@", a);
        
    // 监听
    [self.lanbSio on:@"videoCallRequest" callback:^(NSArray* data) {
        @strongify(self);
        NSString *str = [NSString stringWithFormat:@"接收videoCallRequest:数据:\n%@", data.description];
        NSLog(@"%@" ,str);
        [self.view makeToast:str
        duration:3.f
        position:CSToastPositionCenter];
        
    }];
    
    // 监听
    [self.lanbSio on:@"rechargeNotify" callback:^(NSArray* data) {
        @strongify(self);
        NSString *str = [NSString stringWithFormat:@"接收rechargeNotify:数据:\n%@", data.description];
        NSLog(@"%@" ,str);
        [self.view makeToast:str
        duration:3.f
        position:CSToastPositionCenter];
    }];
    
    NSDictionary *params = @{@"userId":@(18594),
    @"session":@"4ad3b578d48d41f29bb31fe3a4d94244",
    @"version":@"6.3.0",
    @"device":@"iphoen",
    @"productCode": @(1),
    @"deviceNo":lanB_DeviceUUID(),
    };
    
    NSArray *a = @[params.mj_JSONString];
    // 发送
    
//    [self.lanbSio emitWithAck:@"login300" item:a callback:^(NSArray *data) {
//        @strongify(self);
//        NSString *str = [NSString stringWithFormat:@"发送login300:数据:\n%@", data.description];
//        NSLog(@"%@" ,str);
//        [self.view makeToast:str
//        duration:3.f
//        position:CSToastPositionCenter];
//    }];
    
    [self.lanbSio emitWithAck:@"login_online" item:@[@"hi xinxin!"] callback:^(NSArray *data) {
        @strongify(self);
        NSString *str = [NSString stringWithFormat:@"发送login300:数据:\n%@", data.description];
        NSLog(@"%@" ,str);
        [self.view makeToast:str
        duration:3.f
        position:CSToastPositionCenter];
    }];
    
        
}

- (LanBSio *)lanbSio
{
    if (!_lanbSio) {
        
//        http://mgr.vchat.club:9094
//        https://chat.vchat.club:9094
//        http://chat.vchat.club:9194
//    http://18.222.179.127/
        LanBSio *sio = [[LanBSio alloc] initWithUrlString:@"http://18.222.179.127:2120" reconnectAttempts:@(-1) reconnectWait:@(5) config:@{@"log":@1, @"forcePolling":@0, @"voipEnabled":@1, @"forceWebsockets":@1}];
        _lanbSio = sio;
    }
    return _lanbSio;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
